<?php
namespace App\Admin\Controllers\Basics;

use App\Admin\Actions\Basics\FbaWarehouse\ImportFbaWarehouse;
use App\Models\Basics\FbaWarehouse;
use App\Models\Basics\SysCountry;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class FbaWarehouseController extends AdminController{

    protected $title = 'FBA仓库设置';

    protected function grid()
    {
        $grid = new Grid(new FbaWarehouse());
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('country.country','国家');
        $grid->column('d_address_code','编码');
        $grid->column('d_address','地址');

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportFbaWarehouse());
        });

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->equal('country_id', '国家')->multipleSelect(SysCountry::options());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('d_address_code', '编码');
            });
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new FbaWarehouse());

        $form->hidden('id');

        $form->select('country_id', '国家')
            ->options(SysCountry::options())
            ->required();
        $form->text('d_address_code', '编码')
            ->creationRules(['required', "unique:b_fba_warehouse"],['required' => '编码必填','unique' => '编码已存在'])
            ->updateRules(['required', "unique:b_fba_warehouse,d_address_code,{{id}}"],['required' => '编码必填','unique' => '编码已存在']);
        $form->text('d_address','地址')
            ->required();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        return $form;
    }
}
